"""
A contains common plotting constructs
"""
from atlas import web
from atlas.plotting import chart
from atlas.plotting import tracks
from atlas.plotting.const import *

def composite_bartrack( fix, fay, bix, bay, param, name='', opts=None):
    "Makes a composite barplot"
    opts = opts or chart_options( 
        xscale=param.xscale, fgColor=PURPLE, 
        XAxisOnTop=1, bpad=0, edgeColor=GREY, ylabel='Reads', 
        ylabel2='Fit', w=param.width, lw=2, name=name, 
    )
        
    bar = tracks.fitted_data( 
        line_x=fix, line_y=fay, 
        bars_x=bix, bars_y=bay, 
        options=opts, #bar_name='Reads', line_name='Fit' # for legend
    )
    return bar

def twostrand_bartrack( fix, fpy, fmy, bix, bmy, bpy,  param, name='', opts=None):
    "Makes a twostrand barplot"
    opts = opts or chart_options( 
        xscale=param.xscale, XAxisOnTop=1, bpad=0, edgeColor=GREY, ylabel='Reads', 
        ylabel2='Fit', w=param.width, lw=2, name=name
    )
        
    # not so pretty...
    bar = tracks.mirrored_fitted_data( 
        line_upx=fix, line_upy=fpy,
        bars_upx=bix, bars_upy=bpy,
        line_dwx=fix, line_dwy=fmy,
        bars_dwx=bix, bars_dwy=bmy,
        options=opts, plus_name='+ Strand', minus_name='- Strand' # for legend
    )
    return bar

def composite_nuctrack( features, label, param, kdict, ylabel='' ):
    "Makes a composite nucleosome track"
    value = float( param.min_fit )
    subset = web.feature_filter(data=features, name=label, strand='A', value=value, kdict=kdict)
    data  = web.feature2data( subset )
            
    opts = track_options( 
        xscale=param.xscale, w=param.width, fgColor=PURPLE,
        show_labels=param.show_labels, ylabel='', # turn off yaxis label,
        bgColor=WHITE,
    )
    track = tracks.simple_track( data=data, options=opts, name= ylabel + ' Nucleosomes' ) 
    return track 

def twostrand_nuctrack( features, label, param, kdict, ylabel='' ):
    "Makes a two stranded nucleosome track"
    value = float( param.min_fit )
    
    opts = track_options( 
        xscale=param.xscale, w=param.width, fgColor=PURPLE,
        show_labels=param.show_labels, ylabel=ylabel, bgColor=WHITE 
    )

    subset = web.feature_filter(data=features, name=label, value=value, kdict=kdict)
    track = tracks.split_tracks( features=subset, options=opts, split=True, plus_name='+ Nucl', minus_name='- Nucl' ) # name for legend
    return track 

def orftrack( features, label, param, kdict, bgcolor,  ylabel=None,
        fgcolors=[BLUE, RED], track_type='vector', plus_name=None, minus_name=None, h=100 ): # name for legend
    "Makes an mRNA track"
    
    opts  = track_options( 
        xscale=param.xscale, w=param.width, fgColor=PURPLE,
        show_labels=param.show_labels, ylabel=ylabel,#
        bgColor=bgcolor, h=h
    )
    
    subset = web.feature_filter(data=features, name=label, kdict=kdict)

    track = tracks.split_tracks( features=subset, 
        options=opts, split=param.show_labels, colors=fgcolors, 
        track_type=track_type, plus_name=plus_name, minus_name=minus_name ) # name for legend

    return track 

def test( verbose=0 ):
    "Performs module level testing"
    import doctest
    doctest.testmod( optionflags=doctest.ELLIPSIS + doctest.NORMALIZE_WHITESPACE, verbose=verbose )

if __name__ == "__main__":
    test()
